package com.tim.app.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import com.application.library.base.LSAdapter;
import com.application.library.widget.DMGallery;
import com.application.library.widget.SlidingIndicator;
import com.tim.app.R;
import com.tim.app.RT;
import com.tim.app.server.entry.Advertisement;
import com.tim.app.ui.cell.BannerCell;
import com.tim.app.util.ViewGT;

import java.util.List;


public class BannerView extends RelativeLayout implements AdapterView.OnItemClickListener, AdapterView.OnItemSelectedListener {

    private RelativeLayout rl_head;
    private DMGallery gallery;
    private SlidingIndicator indicator;
    private TickerAdapter mAdapter;

    public BannerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        gallery = (DMGallery) findViewById(R.id.gallery);
        indicator = (SlidingIndicator) findViewById(R.id.indicator);
        rl_head = (RelativeLayout) findViewById(R.id.rl_head);
        this.gallery.setOnItemSelectedListener(this);
        this.gallery.setOnItemClickListener(this);
    }

    private List<Advertisement> banners;

    public void setData(List<Advertisement> banners) {
        this.banners = banners;
        if (this.banners == null || this.banners.size() <= 0) {
            rl_head.setLayoutParams(new RelativeLayout.LayoutParams(RT.getScreenWidth(), (int) (RT.getScreenWidth() * 0.4667)));
        }

        mAdapter = new TickerAdapter();
        mAdapter.setData(banners);

        indicator.setCount(banners.size());
        indicator.onItemSelect(0);

        gallery.setAdapter(mAdapter);
        gallery.setSelection(0);

        int pos = 0;
        if (banners.size() > 1) {
            pos = Integer.MAX_VALUE / 2;
            while (pos % banners.size() != 0) {
                pos++;
            }
            this.indicator.setVisibility(View.VISIBLE);
        } else {
            this.indicator.setVisibility(View.INVISIBLE);
        }
        gallery.setSelection(pos);
        if (banners.size() != 0)
            this.indicator.onItemSelect(pos % banners.size());
    }

    @Override
    public boolean hasFocusable() {
        return false;
    }

    public void hide() {
        rl_head.setVisibility(View.GONE);
    }

    public void show() {
        rl_head.setVisibility(View.VISIBLE);
    }

    public void startLoop() {
        if (gallery != null) {
            gallery.startLoop();
        }
    }

    public void stopLoop() {
        if (gallery != null) {
            gallery.removeLoop();
        }
    }

    public void setLoop(boolean isLoop) {
        if (gallery != null) {
            gallery.setLoop(isLoop);
        }
    }

    class TickerAdapter extends LSAdapter<Advertisement> {

        @Override
        public int getCount() {
            if (this.data == null || this.data.size() == 0) {
                return 0;
            }
            if (this.data.size() == 1) {
                return 1;
            }
            return Integer.MAX_VALUE;
        }

        @Override
        public Advertisement getItem(int position) {
            if (data == null || data.size() <= 0) {
                return null;
            }
            return this.data.get(position % data.size());
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflate(getContext(), R.layout.cell_banner, null);
            }
            if (convertView instanceof BannerCell) {
                ((BannerCell) convertView).onGetData(getItem(position), position, this);
            }
            return convertView;
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if (banners == null) {
            return;
        }
        if (position == 0 && banners.size() > 1) {
            int pos = Integer.MAX_VALUE / 2;
            while (pos % banners.size() != 0) {
                pos++;
            }
            gallery.setSelection(pos);
        }

        if (banners.size() > 0) {
            int p = position % banners.size();
            this.indicator.onItemSelect(p);
        }

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Advertisement advertisement = (Advertisement) parent.getAdapter().getItem(position);
        if (advertisement == null) {
            return;
        }
        ViewGT.processAdvertisement(getContext(), advertisement);
    }

}
